<?php
/**
 * Created by PhpStorm.
 * User: wufeng
 * Date: 2021/8/22
 * Time: 22:33
 */

namespace Wufeng\Healthkit\Auth;


use Wufeng\Healthkit\Config;

class Authorize
{
    private $host = 'https://oauth-login.cloud.huawei.com';
    private $auth = 'oauth2';
    private $version = 'v3';
    private $state = 'healthkit';
    private $display = 'page';
    private $redirect_uri = '';
    private $config;

    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    public function setState($value){
        $this->state = $value;
    }

    /**
     * 设置跳转地址
     * @param $url
     * @author wufeng
     * @date 2021/8/25
     */
    public function setRedirectUrl($url){
        $this->redirect_uri = $url;
    }

    public function getCodeWebUrl()
    {
        $baseUrl = implode('/',[$this->host,$this->auth,$this->version,"authorize"]);
        $params =  http_build_query(array(
            "client_id" => $this->config->getAppId(),
            "response_type" => 'code',
            "redirect_uri" => $this->redirect_uri,
            "scope" => implode(" ",[
                "openid",
                "openid https://www.huawei.com/healthkit/sleep.read",
                "https://www.huawei.com/healthkit/stress.read",
            ]),
            "state"=> $this->state,
            "display"=> $this->display,
            // "access_type"=> $this->access_type
        ));
        return $baseUrl.'?'.$params;
    }

    /**
     * 获取accessToken
     * @param $code
     *
     * @return false|string
     * Time: 2021/8/23
     * Author: wufeng
     */
    public function getTokenInfo($code){
        $baseUrl = implode('/',[$this->host,$this->auth,$this->version,"token"]);
        $params = array(
            "grant_type" => 'authorization_code',
            "code" => $code,
            "client_id" => $this->config->getAppId(),
            "client_secret" => $this->config->getSecret(),
            "redirect_uri" => $this->redirect_uri
        );
        $result = $this->request($baseUrl,$params);
        return $result;
    }

    /**
     * 刷新token
     * @param $refreshToken
     * Time: 2021/8/23
     * Author: wufeng
     */
    public function refreshToken($refreshToken){
        $baseUrl = implode('/',[$this->host,$this->auth,$this->version,"token"]);
        $params = array(
            "grant_type" => 'refresh_token',
            "refresh_token" => $refreshToken,
            "client_id" => $this->config->getAppId(),
            "client_secret" => $this->config->getSecret(),
            // "access_type" => ''
        );
        $result = $this->request($baseUrl,$params);
        return $result;
    }

    private function request($url,$param){
        $ch = curl_init();
        $header[] = "Content-Type: application/x-www-form-urlencoded";
        $content = http_build_query($param, "", "&");

        $header[] = "Content-Length: ".strlen($content);
        curl_setopt($ch, CURLOPT_HEADER, true); //setting output include header.
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //setting the transferred content in the header.
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, count($param));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // check the source of the certificate or not.
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // check the source of the certificate or not.
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // setting not output all content if faild automatically
        $response = curl_exec($ch);
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $result = substr($response, $header_size);
        curl_close($ch);
        return $result;
    }
}
